package people.paper.tv.activity.home.view.impl;

import android.content.Context;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.skyworth.util.Util;

import people.paper.tv.R;
import people.paper.tv.activity.home.HomeActivity;
import people.paper.tv.activity.home.view.IFragementCallback;

public class TitleBar extends LinearLayout implements View.OnFocusChangeListener, View.OnKeyListener, View.OnClickListener {
    private TextView ywylTitle, rmrbTitle, ydtjTitle, spttTitle;
    IFragementCallback fragementCallback;
    int hasSelect = -1;

    public TitleBar(Context context) {
        super(context);
        rmrbTitle = createTitle(HomeActivity.RMRB_INDEX, R.string.people_daily);
        addView(rmrbTitle, new LayoutParams(Util.Div(272), Util.Div(120)));
        ywylTitle = createTitle(HomeActivity.YWYL_INDEX, R.string.focus_news);
        addView(ywylTitle, new LayoutParams(Util.Div(272), Util.Div(120)));
        ydtjTitle = createTitle(HomeActivity.YDTJ_INDEX, R.string.suggested_reading);
        addView(ydtjTitle, new LayoutParams(Util.Div(272), Util.Div(120)));
        spttTitle = createTitle(HomeActivity.SPTT_INDEX, R.string.video_headline);
        addView(spttTitle, new LayoutParams(Util.Div(272), Util.Div(120)));
        refreshSelectedTitle(HomeActivity.RMRB_INDEX);
    }


    private TextView createTitle(int id, int text){
        TextView tv = new TextView(getContext());
        tv.setTextSize(Util.Dpi(48));
        tv.setTextColor(getResources().getColor(R.color.title_unfocus));
        tv.setId(id);
        tv.setText(text);
        tv.setFocusable(true);
        tv.setFocusableInTouchMode(true);
        tv.setGravity(Gravity.CENTER);
        tv.setOnFocusChangeListener(this);
        tv.setOnKeyListener(this);
        tv.setOnClickListener(this);
        return tv;
    }

    public void refreshFocusTitle(int id){
        switch (id){
            case HomeActivity.RMRB_INDEX:
                focus(rmrbTitle);
                unfocus(ywylTitle);
                unfocus(ydtjTitle);
                unfocus(spttTitle);
                break;
            case HomeActivity.YWYL_INDEX:
                unfocus(rmrbTitle);
                focus(ywylTitle);
                unfocus(ydtjTitle);
                unfocus(spttTitle);
                break;
            case HomeActivity.YDTJ_INDEX:
                unfocus(ywylTitle);
                unfocus(rmrbTitle);
                focus(ydtjTitle);
                unfocus(spttTitle);
                break;
            case HomeActivity.SPTT_INDEX:
                unfocus(ywylTitle);
                unfocus(rmrbTitle);
                unfocus(ydtjTitle);
                focus(spttTitle);
                break;
        }
    }

    public void refreshSelectedTitle(int id){
        switch (id){
            case HomeActivity.RMRB_INDEX:
                selected(rmrbTitle);
                unfocus(ywylTitle);
                unfocus(ydtjTitle);
                unfocus(spttTitle);
                break;
            case HomeActivity.YWYL_INDEX:
                unfocus(rmrbTitle);
                selected(ywylTitle);
                unfocus(ydtjTitle);
                unfocus(spttTitle);
                break;
            case HomeActivity.YDTJ_INDEX:
                unfocus(ywylTitle);
                unfocus(rmrbTitle);
                selected(ydtjTitle);
                unfocus(spttTitle);
                break;
            case HomeActivity.SPTT_INDEX:
                unfocus(ywylTitle);
                unfocus(rmrbTitle);
                unfocus(ydtjTitle);
                selected(spttTitle);
                break;
        }
    }

    public void setFocusBar(int id){
        if(id == HomeActivity.RMRB_INDEX){
            rmrbTitle.requestFocus();
        }else if(id == HomeActivity.YWYL_INDEX){
            ywylTitle.requestFocus();
        }else if(id == HomeActivity.YDTJ_INDEX){
            ydtjTitle.requestFocus();
        }else if(id == HomeActivity.SPTT_INDEX){
            spttTitle.requestFocus();
        }
    }

    public void focus(TextView tv){
        tv.getPaint().setFakeBoldText(true);
        tv.setTextColor(getResources().getColor(R.color.title_focus));
        tv.setBackgroundResource(R.mipmap.title_focus_bg);
        tv.setPadding(0,0,0,0);
    }

    public void unfocus(TextView tv){
        tv.getPaint().setFakeBoldText(false);
        tv.setTextColor(getResources().getColor(R.color.title_unfocus));
        tv.setBackground(null);
        tv.setPadding(0,0,0,0);
    }



    public void selected(TextView tv){
        hasSelect = tv.getId();
        tv.getPaint().setFakeBoldText(true);
        tv.setTextColor(getResources().getColor(R.color.title_selected));
        tv.setBackgroundResource(R.mipmap.title_selected_bg);
        tv.setPadding(0,0,0,0);
    }

    public void setFragementCallback(IFragementCallback callback){
        fragementCallback = callback;
    }

    @Override
    public void onFocusChange(View view, boolean b) {
        if(b){
            focus((TextView) view);
        }else if(hasSelect != view.getId()){
            unfocus((TextView) view);
        }
    }

    @Override
    public boolean onKey(View v, int keyCode, KeyEvent event) {
        if(event.getAction() == KeyEvent.ACTION_DOWN){
            if(v.equals(rmrbTitle) && keyCode == KeyEvent.KEYCODE_MENU){
                selected((TextView) v);
                fragementCallback.showDataPick();
                return true;
            }else if(keyCode == KeyEvent.KEYCODE_DPAD_UP){
                return true;
            }else if(keyCode == KeyEvent.KEYCODE_DPAD_LEFT){
                if(v.equals(rmrbTitle)){
                    return true;
                }else{
                    if(fragementCallback.prev(v.getId())){
                        unfocus((TextView) v);
                        return true;
                    }else {
                        return true;
                    }
                }
            }else if(keyCode == KeyEvent.KEYCODE_DPAD_RIGHT){
                if(v.equals(spttTitle)){
                    return true;
                }else{
                    if(fragementCallback.next(v.getId())){
                        unfocus((TextView) v);
                        return true;
                    }else {
                        return true;
                    }
                }
            }
            if(keyCode == KeyEvent.KEYCODE_DPAD_DOWN){
                if(fragementCallback.setFragmentFocus(v.getId())){
                    selected((TextView) v);
                }
                return true;
            }
        }
        return false;
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
         if(id != hasSelect) {
            if(fragementCallback.click(hasSelect,id)) {
                refreshSelectedTitle(id);
            }
        }
        if(id == HomeActivity.RMRB_INDEX) {
            fragementCallback.showDataPick();
        }
    }
}
